import enc from "@/utils/aes"
import { isNull } from "@/utils/utils"
import { setLocalStorage, getLocalStorage, removeLocalStorage } from '@/utils/localStorage'
// import { localStorage.setItem, localStorage.getItem, localStorage.removeItem } from '@/utils/localStorage/index.js' // localStorage.setItem, localStorage.getItem, localStorage.removeItem

const RefreshTokenKey = 'YJYY-REFRESHTOKEN'
const TokenKey = 'YJYY-USERTOKEN'
const RefreshExpires = 'YJYY-REFRESHEXPIRES'
const Expires = 'YJYY-EXPIRES'
const UserInfo = 'YJYY-USERINFO'
const userMenu = 'YJYY-USERMENU'
const Tags = 'YJYY-TAGS'
const roomKey = 'YJYY-DEFAULTROOM'
const AppSetting = 'YJYY-APPSETTING'
const AppConfig = 'YJYY-APPCONFIG'

function getHospitalCode() {
  return localStorage.getItem('jjyy-hospitalCode')
}
// // 设置院区级缓存
// function setLocalStorage(key, data, hospitalCode = getHospitalCode()) {
//   // console.log('hospitalCode', hospitalCode)
//   localStorage.setItem(`${key}-${hospitalCode}`, data)
// }
// // 获取院区级缓存
// function getLocalStorage(key, hospitalCode = getHospitalCode()) {
//   // console.log('into getLocalStorage key:', key)
//   if (key == 'YJYY-USERMENU') {
//     let ls_value = localStorage.getItem(`${key}-${hospitalCode}`)
//     // console.log('ls_value', ls_value)
//   }
//   if (key == 'patientSourceToqueryWay') {
//     // console.log('patientSourceToqueryWay')
//     let ls_value = localStorage.getItem(`${key}-${hospitalCode}`)
//     // console.log('ls_value', ls_value)
//   }
//   // setLocalStorage
//   // 院区缓存没有，历史缓存有，先取历史缓存，不然更新到医院，缓存都要重新设置，会被说的
//   if (isNull(localStorage.getItem(`${key}-${hospitalCode}`)) && !isNull(localStorage.getItem(`${key}`))) {
//     return localStorage.getItem(`${key}`) || ''
//   }
//   return localStorage.getItem(`${key}-${hospitalCode}`) || ''
// }
// // 删除院区级缓存
// function removeLocalStorage(key, data) {
//   const hospitalCode = getHospitalCode()
//   if (!isNull(localStorage.getItem(`${key}-${hospitalCode}`))) {
//     localStorage.removeItem(`${key}-${hospitalCode}`)
//   }
//   if (!isNull(localStorage.getItem(`${key}`))) {
//     localStorage.removeItem(`${key}`)
//   }
// }


function getInfo() {
  const info = decodeURIComponent(localStorage.getItem(UserInfo))
  const infoObj = info != 'undefined' ? info : null
  return infoObj
}
function getUserMenu() {
  // 就这样，没事不要轻易改，没医院代码之前就已经存储菜单列表了
  const info = localStorage.getItem(userMenu) || ''
  return info
}
function getTagsL() {
  const tags = localStorage.getItem(Tags)
  const _tags = tags != 'undefined' ? tags : ''
  return _tags
}
export const getDefaultRoom = function () {
  const room = getLocalStorage(roomKey)
  const _room = room && room !== null && room !== 'undefined' ? room.split(',') : []
  return _room
}
export function setDefaultRoom () {
  setLocalStorage(roomKey, '')
}
// 获取登录的时候就要存储的信息，无需根据医院代码来存储
const getLocalInfo = function (key) {
  const _info = localStorage.getItem(key)
  const infoStr = !_info ? null : aesDec(_info)
  // console.log('infoStr', JSON.parse(infoStr))
  return infoStr
}
// 加密
const aesEnc = function (val) {
  return decodeURIComponent(enc.AESEncrypt(val))
}
// 解密
const aesDec = function (val) {
  return enc.AESDecrypt(decodeURIComponent(val))
}
// Token，无需根据医院代码来存储
export function getToken() {
  return localStorage.getItem(TokenKey)
}
export function setToken(token) {
  return localStorage.setItem(TokenKey, token)
}
export function removeToken() {
  return localStorage.removeItem(TokenKey)
}
// RefreshToken
export function getRefreshToken() {
  return localStorage.getItem(RefreshTokenKey)
}
export function setRefreshToken(refreshToken) {
  return localStorage.setItem(RefreshTokenKey, refreshToken)
}
export function removeRefreshToken() {
  return localStorage.removeItem(RefreshTokenKey)
}
// Token 时间戳
export function setExpires(expires) {
  return localStorage.setItem(Expires, expires)
}
export function getExpires() {
  return localStorage.getItem(Expires)
}
export function removeExpires() {
  return localStorage.removeItem(Expires)
}
// Token 过期时间戳
export function setRefreshExpires(expires) {
  return localStorage.setItem(RefreshExpires, expires)
}
export function getRefreshExpires() {
  return localStorage.getItem(RefreshExpires)
}
export function removeRefreshExpires() {
  return localStorage.removeItem(RefreshExpires)
}
// 用户信息
export function getUserInfo() {
  return getInfo()
}
export function setUserInfo(info) {
  return localStorage.setItem(UserInfo, info)
}
export function removeUserInfo() {
  return localStorage.removeItem(UserInfo)
}
// 后端维护的应用配置信息
export function getAppSetting() {
  return getLocalInfo(AppSetting)
}
export function setAppSetting(info) {
  return localStorage.setItem(AppSetting, aesEnc(info))
}
export function removeAppSetting() {
  return localStorage.removeItem(AppSetting)
}
// 项目的配置信息
export function getAppConfig() {
  return getLocalInfo(AppConfig)
}
export function setAppConfig(info) {
  return localStorage.setItem(AppConfig, aesEnc(info))
}
export function removeAppConfig() {
  return localStorage.removeItem(AppConfig)
}
// 用户菜单
export function getMenu() {
  // console.log('getUserMenu=>userMenu', userMenu)
  // 就这样，没事不要轻易改，没医院代码之前就已经存储菜单列表了
  let resultUserMenu = localStorage.getItem('YJYY-USERMENU') || ''
  // console.log('resultUserMenu', resultUserMenu)
  return resultUserMenu
}
export function setMenu(info) {
  // 就这样，不要轻易改，存储菜单列表之前还没获取医院代码
  return localStorage.setItem(userMenu, info)
}
export function removeMenu() {
  return removeLocalStorage(userMenu)
}
// 用户操作权限
export function getTags() {
  return getTagsL()
}
export function setTags(tags) {
  return localStorage.setItem(Tags, tags)
}
export function removeTags() {
  return removeLocalStorage(Tags)
}
// 医院/院区/部门/科室/机房 数组，用户排班选择机房的默认参数管理
export function getDefaultCheckRoom() {
  return getDefaultRoom()
}
export function setDefaultCheckRoom(hospitalRoom) {
  return setLocalStorage(roomKey, hospitalRoom)
}
export function removeDefaultCheckRoom() {
  return removeLocalStorage(roomKey)
}

// 设置
export function getLocalStorageKey(key) {
  return getLocalStorage(key)
}
export function setLocalStorageKey(key, info) {
  return setLocalStorage(key, info)
}
export function removeLocalStorageKey(key) {
  return removeLocalStorage(key)
}
const lastRequestTime = 'lastRequestTime'
// 设置访问时间过长应强制注销的问题
export function getOperateTime() {
  return localStorage.getItem(lastRequestTime);
}

export function setOperateTime(time) {
  localStorage.setItem(lastRequestTime, time.toString());
}

export function removeOperateTime() {
  localStorage.removeItem(lastRequestTime);
}
const logOutFlag = 'logoutFlag'
export function getLogoutFlag() {
  return localStorage.getItem(logOutFlag);
}

export function setLogoutFlag(flag) {
  localStorage.setItem(logOutFlag, flag.toString());
}

export function removeLogoutFlag() {
  localStorage.removeItem(logOutFlag);
}
